package cn.com.miaoto.common;

import cn.com.miaoto.model.LoginUser;
import com.alibaba.fastjson.annotation.JSONField;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;

public class SessionUserDetails implements UserDetails {

    private final Collection<? extends GrantedAuthority> authorities;

    private Integer id;
    private String userId;
    private String userName;
    private String pwd;
    private String email;
    private String job;
    private String status;

    public SessionUserDetails(LoginUser userinfo) {
        this.authorities = new ArrayList<GrantedAuthority>() {
            {
                this.add(new SimpleGrantedAuthority("ROLE_1"));
            }
        };
        this.setUserId(userinfo.getUserId());
        this.setPwd(userinfo.getPassword());
        this.setUserName(userinfo.getUserName());
        this.setEmail(userinfo.getEmail());
        this.setJob(userinfo.getJob());
        this.setStatus(userinfo.getStatus());
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    @JSONField(serialize = false)
    public String getPassword() {
        return this.getPwd();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return (this.getStatus().equals("1"));
    }

    @Override
    public String getUsername() {
        return this.userName;
    }


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}
